package com.baidu.jiuhao.es.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baidu.jiuhao.es.common.Result;
import com.baidu.jiuhao.es.req.DocSearchRequest;
import com.baidu.jiuhao.es.req.DocumentReq;
import com.baidu.jiuhao.es.rsp.DocumentRsp;
import com.baidu.jiuhao.es.service.DocumentSearchService;
import com.baidu.jiuhao.es.service.SearchDocService;
import com.baidu.jiuhao.es.service.SearchFaqervice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author baidu-106
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("api")
@Slf4j
public class DocumentController {

    private final DocumentSearchService searchService;

    private final SearchDocService searchDocService;

    private final SearchFaqervice searchFaqervice;


    /**
     * 排行榜
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("search")
    public ResponseEntity<Result<List<DocumentRsp>>> search(@RequestBody DocumentReq request) throws Exception {
        log.info("search请求入参req:{}", JSONUtil.toJsonStr(request));
        try {
            List<Map<String, Object>> list = searchService.searchDocuments(request);
            if (CollUtil.isNotEmpty(list)) {
                List<DocumentRsp> collect = list.stream().map(v -> {
                    DocumentRsp documentRsp = BeanUtil.toBean(v, DocumentRsp.class);
                    return documentRsp;
                }).collect(Collectors.toList());
                return Result.success(collect);
            } else {
                return Result.success(ListUtil.empty());
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(500, e.getMessage());
        }

    }


    /**
     * doc / video / web /  检索 检索结果根据 document_id 聚合 or 去重
     * {
     * "agentId": "6e82bc75-f6d5-4d9d-b1ed-89150bb113bc",
     * "keyword": "《跨越不可能》[美]史蒂芬·科特勒",
     * "pageNum": 1,
     * "pageSize": 10,
     * // "dirId": "0",
     * // "version": 0,
     * // "labels": [
     * //   "85cd2923-59ad-437b-a887-d601470c64ad"
     * // ]
     * "indexs": [
     * "unit8_knowledge_test_doc_offline_1",
     * "unit8_knowledge_test_doc_offline_1001",
     * "unit8_knowledge_test_video_offline_1",
     * "unit8_knowledge_test_video_offline_1001",
     * "unit8_knowledge_test_web_offline_1",
     * "unit8_knowledge_test_web_offline_1001"
     * ]
     * }
     *
     * @param request
     * @return
     */
    @PostMapping("/docs")
    public ResponseEntity<Result<JSONObject>> searchDocs(@RequestBody DocSearchRequest request) {
        try {
            return Result.success(searchDocService.searchDocs(request));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * faq检索  每条均唯一，不需要聚合
     * {
     * "agentId": "60f5cc98-c399-483c-9dc0-522835e70473",
     * "keyword": "广西的什么粉好吃",
     * // "dirId": "faq_battery",
     * "version": 1,
     * "pageSize": 10,
     * "pageNum": 1,
     * "indexs": [
     * "unit8_knowledge_test_faq_offline_1001",
     * "unit8_knowledge_test_faq_offline_1"
     * ]
     * }
     *
     * @param request
     * @return
     */
   /* @PostMapping("/faqs")
    public ResponseEntity<Result<FaqSearchResponse>> searchFaqs(@RequestBody FaqSearchRequest request) {
        try {
            return Result.success(searchFaqervice.searchFaqs(request));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(500, e.getMessage());
        }
    }*/
}
